﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.shop
{
    public partial class mycoupon : MxWeiXinPF.Web.UI.UserPage
    {
        public string carturl = "";
        public string indexurl = "";
        protected int page;
        protected int pageSize;
        protected int totalCount;
        protected int uid = 0;
        public string url = "";
        protected MxWeiXinPF.Model.users user_info;
        public string userinfourl = "";
        public string categoryurl = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                base.wid = MyCommFun.RequestInt("wid");
                base.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                this.uid = MyCommFun.RequestInt("uid");
                this.pageSize = this.GetPageSize(10);
                if (this.uid == 0)
                {
                    if (HttpContext.Current.Session["dt_session_user_info"] != null)
                    {
                        this.user_info = HttpContext.Current.Session["dt_session_user_info"] as MxWeiXinPF.Model.users;
                        this.uid = this.user_info.id;
                    }
                    else
                    {
                        MxWeiXinPF.Model.users modelByOpenid = new MxWeiXinPF.Model.users();
                        modelByOpenid = new MxWeiXinPF.BLL.users().GetModelByOpenid(base.openid);
                        this.uid = modelByOpenid.id;
                    }
                    if (!this.Page.IsPostBack)
                    {
                        this.RptBind(this.uid);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected string CombSqlTxt(int uid)
        {
            StringBuilder builder = new StringBuilder();
            if (!string.IsNullOrEmpty(base.openid))
            {
                builder.Append(" and a.uid='" + uid + "' ");
            }
            return builder.ToString();
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("mycoupon_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }
        //protected void txtPageNum_TextChanged(object sender, EventArgs e)
        //{
        //    int num;
        //    if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
        //    {
        //        Utils.WriteCookie("mycoupon", num.ToString(), 0x3840);
        //    }
        //    base.Response.Redirect(Utils.CombUrlTxt("mycoupon.aspx", "uid={0}", new string[] { this.uid.ToString() }));
        //}
        private void RptBind(int uid)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            this.rptList.DataSource = new MxWeiXinPF.BLL.wx_shop_coupon_data().GetCouponByUid(uid);
            this.rptList.DataBind();
            //this.txtPageNum.Text = this.pageSize.ToString();
            //string linkUrl = Utils.CombUrlTxt("mycoupon.aspx", "uid={0}", new string[] { this.uid.ToString() });
           // this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        public string GetState(string usestate,string couponstate,string timeend)
        {
            DateTime end = Convert.ToDateTime(timeend);
            if (end < DateTime.Now)
            {
                return "已过期";
            }
            else 
            {
                if (couponstate != "1")
                {
                    return "已过期";
                }
                else {
                    if (usestate == "1")
                    {
                        return "已使用";
                    }
                    else { return "未使用"; }
                }
            }
           
        }

        public string GetImg(string usestate, string couponstate, string timeend, string thumb, string usedthumb, string expiredthumb)
        {
            DateTime end = Convert.ToDateTime(timeend);
            if (end < DateTime.Now)
            {
                return MyCommFun.getWebSite() + expiredthumb;
            }
            else
            {
                if (couponstate != "1")
                {
                    return MyCommFun.getWebSite() + expiredthumb;
                }
                else
                {
                    if (usestate == "1")
                    {
                        return MyCommFun.getWebSite() + usedthumb;
                    }
                    else { return MyCommFun.getWebSite() + thumb; }
                }
            }

        }
        public string GetImg(string couponid)
        {
            Model.wx_shop_coupon_data model = new BLL.wx_shop_coupon_data().GetModel(Convert.ToInt32(couponid));
            if (model == null)
            {
                return "";
            }
            Model.wx_shop_coupon mcoupon = new BLL.wx_shop_coupon().GetModel(model.couponid);
            if (mcoupon == null)
            {
                return "";
            }
            if (model.status == 0)
            {
                if (mcoupon.timeend <= DateTime.Now)
                {
                    return  MyCommFun.getWebSite() + mcoupon.expiredthumb;
                }
                return MyCommFun.getWebSite() + mcoupon.thumb;
            }
            else if (model.status == 1)
            {
                return MyCommFun.getWebSite() + mcoupon.usedthumb;
            }
            return MyCommFun.getWebSite() + mcoupon.expiredthumb;
        }

    }
}